package com.example.yorkfeng.alarmdemo;

import android.app.Activity;
import android.app.AlertDialog;
import android.content.Context;
import android.content.DialogInterface;
import android.media.AudioManager;
import android.media.MediaPlayer;
import android.os.Bundle;

public class AlarmActivity extends Activity {

    private MediaPlayer mediaPlayer;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_alarm);

        System.out.println("Alarm:"+System.currentTimeMillis());

        final AudioManager mAudioManager = (AudioManager) getSystemService(Context.AUDIO_SERVICE);
        final int max = mAudioManager.getStreamMaxVolume( AudioManager.STREAM_MUSIC );
        final int current = mAudioManager.getStreamVolume(AudioManager.STREAM_MUSIC);
        mAudioManager.setStreamVolume(AudioManager.STREAM_MUSIC, max, 0);

        mediaPlayer = MediaPlayer.create(this, R.raw.air);
        mediaPlayer.setLooping(true);
        mediaPlayer.start();

        //创建一个闹钟提醒的对话框,点击确定关闭铃声与页面
        new AlertDialog.Builder(this).setTitle("Alarm").setMessage("闹钟时间到了")
        .setPositiveButton("Close", new DialogInterface.OnClickListener() {
            @Override
            public void onClick(DialogInterface dialog, int which) {
                mediaPlayer.stop();
                mAudioManager.setStreamVolume(AudioManager.STREAM_MUSIC, current, 0);
                finish();
            }
        }).show();
    }
}
